package com.defect.service.managementSercvice.managementSercviceImpl;

import com.defect.bo.*;
import com.defect.mapper.*;
import com.defect.po.*;
import com.defect.service.managementSercvice.BaseInfoService;
import com.defect.util.pageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BaseInfoServiceImpl implements BaseInfoService {
    @Autowired
    private BaseInfoMapper baseInfoMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private StateMapper stateMapper;
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private SeverityMapper severityMapper;
    @Autowired
    private PriorityMapper priorityMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserIssueRelMapper userIssueRelMapper;
    @Autowired
    private WorkLogMapper workLogMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public PageDetail searchBaseInfoList(BaseInfoListSearch baseInfoListSearch, Integer pageIndex, Integer pageSize) {
        List<BaseInfo> list =  baseInfoMapper.searchBaseInfoList(baseInfoListSearch);
        PageDetail pageInfo = pageUtil.getPageInfo(pageIndex, pageSize, list);
        return pageInfo;
    }
//这里是获取当前用户缺陷列表
    @Override
    public PageDetail searchUserBaseInfoList(UserBaseInfoListSearch userBaseInfoListSearch, Integer pageIndex, Integer pageSize, int userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        String assignee = user.getUsername();
        List<Integer> issueIdList = userIssueRelMapper.getIssueIdList(userId);
        List<BaseInfo> list =  baseInfoMapper.getUserTask(issueIdList,userBaseInfoListSearch);
        PageDetail pageInfo = pageUtil.getPageInfo(pageIndex, pageSize, list);
        return pageInfo;
    }
//    获取反馈列表
    @Override
    public PageDetail searchCommentBaseInfoList(UserBaseInfoListSearch userBaseInfoListSearch, Integer pageIndex, Integer pageSize) {

        List<BaseInfo> list =  baseInfoMapper.searchCommentBaseInfoList(userBaseInfoListSearch);
        PageDetail pageInfo = pageUtil.getPageInfo(pageIndex, pageSize, list);
        return pageInfo;
    }

    @Override
    public int commentDeal(CommentBo commentBo) {
        //改变issueState
        baseInfoMapper.updatIssueState(commentBo);
//        添加comment记录
        Comment comment = new Comment();
        comment.setIssueId(commentBo.getIssueId());
        comment.setComment(commentBo.getComment());
        comment.setLogState(commentBo.getIssueState());
        comment.setCreateTime(new Date());
//        还需要添加createby
        int i = commentMapper.insert(comment);

        return i;
    }


    @Override
    public int commitDeal(BaseInfoDealItem baseInfoDealItem) {
//        记录工作日志
        WorkLog workLog = new WorkLog();
        workLog.setIssueId(baseInfoDealItem.getIssueId());
        workLog.setCreateTime(new Date());
        workLog.setLogState(baseInfoDealItem.getIssueState());
        workLog.setWorkHours(baseInfoDealItem.getWorkHours());
        workLog.setLogDate(baseInfoDealItem.getLogDate());
//        createBY放入登陆用户
         int i = workLogMapper.insert(workLog);
//         累计工时
        BigDecimal workhours=  baseInfoMapper.getWorkHours(baseInfoDealItem.getIssueId());
        BigDecimal add = baseInfoDealItem.getWorkHours().add(workhours);
        baseInfoDealItem.setWorkHours(add);
        int b = baseInfoMapper.dealTask(baseInfoDealItem);
        return b;
    }

    @Override
    public List<WorkLog> getLogByIssueId(Integer issueId) {
        return commentMapper.getLogByIssueId(issueId);
    }




    @Override
    public int delTaskArray(ArrayList<Integer> list) {
        return baseInfoMapper.delTaskArray(list);
    }

    @Override
    public int delTask(int issueId) {
        return baseInfoMapper.delTask(issueId);
    }

    @Override
    public List<Project> getAllProjects() {
        return projectMapper.getAllProjects();
    }

    @Override
    public List<State> getAllStates() {
        return stateMapper.getAllStates();
    }

    @Override
    public List<BaseInfo> getAllIssues() {
        return baseInfoMapper.getAllIssues();
    }

    @Override
    public List<Type> getAllType() {
        return typeMapper.getAllType();
    }

    @Override
    public List<Severity> getAllSeveritys() {
        return severityMapper.getAllSeveritys();
    }

    @Override
    public List<Priority> getAllPrioritys() {
        return priorityMapper.getAllPrioritys();
    }

    @Override
    public BaseInfo checkIssueName(String issueName) {
        return baseInfoMapper.checkIssueName(issueName);
    }

    @Override
    public int addBaseInfo(BaseInfo baseInfo) {
        return baseInfoMapper.addBaseInfo(baseInfo);
    }

    @Override
    public int getId() {
        return baseInfoMapper.lastId()+1;
    }

    @Override
    public int changeBaseInfo(BaseInfo baseInfo) {
        return baseInfoMapper.changeBaseInfo(baseInfo);
    }

    @Override
    public List<User> getUserArray(BaseInfo baseInfo) {
        List<UserProjectIdBo> userProjectBoArray = userMapper.getUserProjectBoArray(baseInfo);
        ArrayList<User> users = new ArrayList<>();
        String projectId = baseInfo.getProjectId();
        for (int i=0;i<userProjectBoArray.size();i++){
            UserProjectIdBo userProjectIdBo = userProjectBoArray.get(i);
            if (projectId.equals(userProjectIdBo.getProjectId())){
                User user = new User();
                user.setUserId(userProjectIdBo.getUserId());
                user.setUserName(userProjectIdBo.getUsername());
                users.add(user);
            }
        }
        return users;
    }

    @Override
    public int commitAllocation(Integer issueId, Integer userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        baseInfoMapper.allocate(issueId,user.getUsername());
       return userIssueRelMapper.allocateRel(issueId,userId);

    }



}
